package com.salesmanager.web.admin.controller.orders;

import org.apache.commons.lang3.StringUtils;

import com.salesmanager.core.business.order.model.OrderType;
import com.salesmanager.core.business.order.model.orderstatus.OrderStatus;

public class OrderUtils {
	public static final Integer INCREASE_QUANTITY = 1;
	public static final Integer DECREASE_QUANTITY = -1;
	public static final Integer NOACTION_QUANTITY = 0;
	
	public static int quantityAction(OrderStatus oldStatus, OrderStatus newStatus, OrderType type){
		int factor = 1;
		// quantity is opposite for order and import type
		if(OrderType.IMPORT.equals(type)){
			factor = -1;
		}
		boolean oldUpdateQtt = inUpdateQuantity(oldStatus);
		boolean newUpdateQtt = inUpdateQuantity(newStatus);
		
		if(oldUpdateQtt && newUpdateQtt){
			return NOACTION_QUANTITY;
		}
		
		if(!oldUpdateQtt && !newUpdateQtt){
			return NOACTION_QUANTITY;
		}
		
		if(!oldUpdateQtt && newUpdateQtt){
			return factor * DECREASE_QUANTITY;
		}
		
		if(oldUpdateQtt && !newUpdateQtt){
			return factor * INCREASE_QUANTITY;
		}
		
		return 0;
	}
	
	public static boolean inUpdateQuantity(OrderStatus orderStatus){
		return OrderStatus.SHIPPED.equals(orderStatus)
				|| OrderStatus.COMPLETED.equals(orderStatus);
	}
	
	public static boolean inUpdateReserve(OrderStatus orderStatus){
		return !OrderStatus.SHIPPED.equals(orderStatus)
				&& !OrderStatus.COMPLETED.equals(orderStatus);
	}


	private String getEmptyIfNull(String input){
		return StringUtils.isEmpty(input) ? "" : input;
	}
}
